function Promise(fn) {
  var state = 'pending',
      value = null,
      callbacks = [];

    this.then = function(onFulfilled) {
      if (state === 'pending') {
        callbacks.push(onFulfilled);
        return this
      }
      onFulfilled(value)
      return this
    }


    function resolve(newValue) {
      value = newValue
      state = 'fulfilled';
      setTimeout(function() {
        callbacks.forEach(function(callback) {
          callback(value)
        })
      }, 0)
    }    

    fn(resolve)
}




function getUserId() {
  return new Promise(function(resolve) {
    resolve(10)
  })
}

getUserId().then(function(id) {
  return id + 5
}).then(function(key) {
  console.log(key)
})